package com.hui.filter;

import com.hui.dao.MenuDao;
import com.hui.entity.Manager;
import com.hui.entity.Menu;
import com.hui.service.MenuService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Description 登录拦截器
 * @date 2023/2/10 15:46
 */
@WebFilter(urlPatterns = {"/*"})
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resq = (HttpServletResponse) servletResponse;


        /**
         *需要放行登录请求以及登录页面的静态资源和验证码请求
         **/
        String uri = req.getRequestURI();
        if (uri.endsWith("login") || uri.endsWith(".css") || uri.endsWith(".js")
                || uri.endsWith(".png") || uri.endsWith("CheckCodeServlet") || uri.endsWith("/")) {
            filterChain.doFilter(req, servletResponse);
            return;
        }

        HttpSession session = req.getSession();

        if (session.getAttribute("currentUser") == null) {
            req.setAttribute("msg", "请先登录");
            req.getRequestDispatcher("login.jsp").forward(req, resq);
        } else {
            // 获取当前用户
            Manager manager = (Manager) session.getAttribute("currentUser");
            MenuService service = new MenuService();
            // 根据当前用户id获取菜单
            List<Menu> list = service.findMenuById(manager.getMgrId());
            MenuDao dao = new MenuDao();
            List<Menu> list2 = dao.findByMenuLevel(2);//获取二级菜单
            String url = "";
            boolean islist2=false;//判断当前请求的url是否包含在二级菜单中
            boolean islist1=false;//判断当前请求的url是否包含在用户可访问的菜单中
            for (Menu menu : list2) {
                if(uri.endsWith(menu.getMenuUrl())){//判断当前请求的url是否包含在二级菜单中
                    islist2=true;
                    break;
                }
            }
            boolean flag = false;
            if(islist2){//如果包含在二级菜单中
                for (Menu menu : list) {//遍历用户可访问的菜单
                    List<Menu> mlist=menu.getChildList();
                    if (mlist.size()>0){
                        for (Menu m:mlist){
                            if(uri.endsWith(m.getMenuUrl())){//判断当前请求的url是否包含在二级菜单中
                                flag = true;
                                islist1=true;
                                break;
                            }
                        }
                    }

                    if (flag){
                        break;
                    }

                }
                if(islist1){//url在2级菜单中并且该用户有权限
                    //设置请求参数和响应到浏览器编码
                    req.setCharacterEncoding("utf-8");
                    resq.setContentType("text/html;charset=utf-8");
                    filterChain.doFilter(req, resq);
                    return;
                }
                //url在菜单中但是该用户没有该菜单权限
                req.setAttribute("msg", "没有权限");
                req.getRequestDispatcher("login.jsp").forward(req, resq);
            }
            //访问url不在菜单中比如前端静态资源请求之类的放行
            //设置请求参数和响应到浏览器编码
            req.setCharacterEncoding("utf-8");
            resq.setContentType("text/html;charset=utf-8");
            //放行
            filterChain.doFilter(req, resq);
        }

    }

    @Override
    public void destroy() {

    }
}
